package com.shahe.GirlsDay;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.TabHost.OnTabChangeListener;

import com.shahe.GirlsDay.MyView.*;

/**
 * Created by Sarah on 5/06/2014.
 */
public class ChartActivity extends Activity {
	/**
	 * Called when the activity is first created.
	 */

	private LinearLayout layout_chart;
	private ChartView chartView;
	private CycleChartView cycleChartView;
    private DurationChartView durationChartView;
    private WeightChartView weightChartView;
    private BBTChartView bbtChartView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chartpage);

		layout_chart = (LinearLayout) findViewById(R.id.chartArea);
		//chartView = new ChartView(this);
		cycleChartView = new CycleChartView(this);
        durationChartView = new DurationChartView(this);
        weightChartView = new WeightChartView(this);
        bbtChartView = new BBTChartView(this);
		layout_chart.removeAllViews();
		layout_chart.addView(cycleChartView);

		final Button homeButton = (Button) findViewById(R.id.btn_home);

		homeButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
                finish();
			}
		});

		TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
		tabHost.setup();

		final TabWidget tabWidget = tabHost.getTabWidget();
		final FrameLayout tabContent = tabHost.getTabContentView();

		// Get the original tab textviews and remove them from the viewgroup.
		TextView[] originalTextViews = new TextView[tabWidget.getTabCount()];
		for (int index = 0; index < tabWidget.getTabCount(); index++) {
			originalTextViews[index] = (TextView) tabWidget
					.getChildTabViewAt(index);
		}
		tabWidget.removeAllViews();

		// Ensure that all tab content childs are not visible at startup.
		for (int index = 0; index < tabContent.getChildCount(); index++) {
			tabContent.getChildAt(index).setVisibility(View.GONE);
		}

		// Create the tabspec based on the textview childs in the xml file.
		// Or create simple tabspec instances in any other way...
		for (int index = 0; index < originalTextViews.length; index++) {
			final TextView tabWidgetTextView = originalTextViews[index];
			final View tabContentView = tabContent.getChildAt(index);
			TabHost.TabSpec tabSpec = tabHost
					.newTabSpec((String) tabWidgetTextView.getTag());
			tabSpec.setContent(new TabHost.TabContentFactory() {
				@Override
				public View createTabContent(String tag) {
					return tabContentView;
				}
			});
			if (tabWidgetTextView.getBackground() == null) {
				tabSpec.setIndicator(tabWidgetTextView.getText());
			} else {
				tabSpec.setIndicator(tabWidgetTextView.getText(),
						tabWidgetTextView.getBackground());
			}
			tabHost.addTab(tabSpec);
		}
		tabHost.setCurrentTab(0);

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            TextView textView_l1 = (TextView)findViewById(R.id.tv_chart_l1);
            TextView textView_l2 = (TextView)findViewById(R.id.tv_chart_l2);
            TextView textView_m1 = (TextView)findViewById(R.id.tv_chart_m1);
            TextView textView_m2 = (TextView)findViewById(R.id.tv_chart_m2);
            TextView textView_r1 = (TextView)findViewById(R.id.tv_chart_r1);
            TextView textView_r2 = (TextView)findViewById(R.id.tv_chart_r2);

			@Override
			public void onTabChanged(String tabId) {

				// Toast.makeText(getApplicationContext(), tabId,
				// Toast.LENGTH_SHORT).show();
				switch (tabId) {
				case "tab0":
					layout_chart.removeAllViews();
					layout_chart.addView(cycleChartView);
                    //textView_l1.setText();
                    textView_l2.setText(R.string.chart_short_days);
                    //textView_m1.setText();
                    textView_m2.setText(R.string.chart_long_days);
                    //textView_r1.setText();
                    textView_r2.setText(R.string.chart_avg_days);

					break;
				case "tab1":
					layout_chart.removeAllViews();
					layout_chart.addView(durationChartView);
                    //textView_l1.setText();
                    textView_l2.setText(R.string.chart_short_days);
                    //textView_m1.setText();
                    textView_m2.setText(R.string.chart_long_days);
                    //textView_r1.setText();
                    textView_r2.setText(R.string.chart_avg_days);
					break;
				case "tab2":
					layout_chart.removeAllViews();
					layout_chart.addView(bbtChartView);
                    textView_l2.setText(R.string.chart_low_temp);
                    textView_m1.setText(R.string.blank);
                    textView_m2.setText(R.string.blank);
                    textView_r2.setText(R.string.chart_high_temp);
					break;
				case "tab3":
					layout_chart.removeAllViews();
					layout_chart.addView(weightChartView);
                    textView_l2.setText(R.string.chart_light_wei);
                    textView_m1.setText(R.string.blank);
                    textView_m2.setText(R.string.blank);
                    textView_r2.setText(R.string.chart_heavy_wei);
					break;					
				}
			}
		});

	}

}
